/**
 * 
 */
package suanfa.linklist;

/**
 * yuexiaowei 2022年4月28日 下午9:57:39
 */
public class Test {

	public static void removeDup(LNode head) {
		if (head == null || head.next == null) {
			return;
		}
		LNode outerCur = head.next;
		LNode innerCur = null;
		LNode innerPre = null;

		for (; outerCur != null; outerCur = outerCur.next) {
			for (innerCur = outerCur.next, innerPre = outerCur; innerCur != null;) {

				if (outerCur.data == innerCur.data) {
					innerPre.next = innerCur.next;
					innerCur = innerCur.next;
				} else {
					innerPre = innerCur;
					innerCur = innerCur.next;
				}
			}
		}
	}

	/**
	 * 计算两个链表的相加 yuexiaowei
	 * 
	 * @param node1
	 * @param node2
	 * @return 2022年4月30日 下午9:53:57
	 */
	public LNode addTwoNumbers(LNode node1, LNode node2) {
		int carry = 0;
		LNode head = null, tail = null;
		while (node1 != null && node2 != null) {
			int a = node1 != null ? node1.data : 0;
			int b = node2 != null ? node2.data : 0;

			int sum = a + b + carry;

			if (head == null) {
				head = tail = new LNode(sum % 10);

			} else {
				tail.next = new LNode(sum % 10);
				tail = tail.next;
			}
			// 取除数
			carry = sum / 10;
			if (node1 != null) {
				node1 = node1.next;
			}
			if (node2 != null) {
				node2 = node2.next;
			}
		}
		// 如果最后一位有进位，新增出来一个节点
		if (carry > 0) {
			tail.next = new LNode(carry);
		}
		// 为什么返回的是这个
		return head;

	}

	public static LNode deleteDuplicates(LNode head) {
		if (head == null) {
			return head;
		}
		LNode cur = head;
		while (cur.next != null) {
			if (cur.data == cur.next.data) {
				// cur.data = cur.next.data;
				cur.next = cur.next.next;
			} else {
				cur = cur.next;
			}
		}

		return head;
	}

	public static void main(String[] args) {
		int i = 1;
		LNode head = new LNode();

		head.next = null;
		LNode tmp = null;
		LNode cur = head;
		for (; i < 7; i++) {
			tmp = new LNode();
			if (i % 2 == 0) {
				tmp.data = i + 1;
			} else if (i % 3 == 0) {
				tmp.data = i - 2;
			} else {
				tmp.data = i;
			}

			tmp.next = null;
			cur.next = tmp;
			cur = tmp;
		}

		System.out.print("删除重复节点前");

		for (cur = head.next; cur != null; cur = cur.next) {
			System.out.print(cur.data + " ");
		}
		System.out.println();
		System.out.print("删除重复节点后");
		// removeDup(head);
		deleteDuplicates(head);
		for (cur = head.next; cur != null; cur = cur.next) {
			System.out.print(cur.data + " ");
		}
	}
}

class LNode {
	int data;
	LNode next;

	public LNode() {
	}

	public LNode(int data) {
		this.data = data;
	}
}
